package thread.learn;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @author jiangwz
 * @create 2019/9/30.
 */
public class MockConcurrency {

    volatile List<String> lists = Collections.synchronizedList(new ArrayList<>());

    public void process(String msg) {
        lists.add(msg);
//        System.out.println(lists);
        if (lists.size() >= 500) {
            System.out.println(Thread.currentThread().getName());
            synchronized (lists) {
                if (lists.size() != 0) {
                    System.out.println("77777");
                    lists.clear();
                    return;
                } else {
                    System.out.println("list已经被处理了");
                }
            }
            System.out.println("list已经被处理了1111");
        }
    }

    public /*synchronized*/ void process2(String msg) {
        synchronized (lists) {
            lists.add(msg);
            if (lists.size() >= 500) {
                System.out.println(Thread.currentThread().getName());
                lists.clear();
            }
        }

    }


    public static void main(String[] args) {
        MockConcurrency mock = new MockConcurrency();
        ExecutorService executorService = Executors.newCachedThreadPool();
        for (int i = 0; i < 50; i++) {
            int finalI = i;
            new Thread(() -> {
                for (int j = 0; j < 15; j++) {
                    int finalJ = j;
                    executorService.submit(() -> {

                        mock.process2(finalI + "-" + finalJ);
                    });
                }
            }, "thread-name").start();
        }

    }
}
